package service

import (
	"gin/common"
	Mysql "gin/database"
	"gin/model"
	nlpWord "gin/tai/dict/word"
	"gin/validate"
	nlp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/nlp/v20190408"
	"gorm.io/gorm"
)

func CreateDictWord(Data validate.AddDictWord) (resData model.DictWord, err error) {
	var dictModel model.DictWord

	Mysql.DB.Where(&model.DictWord{}).Where("word = ?", Data.Word).Where("dict_id = ?", Data.DictId).Take(&dictModel)

	// 重复数据 直接返回
	if dictModel.Id > 0 {
		return dictModel, nil
	}

	dictModel.Word = Data.Word
	dictModel.Pos = Data.Pos
	dictModel.DictId = Data.DictId
	dictModel.ExtendCode = Data.ExtendCode

	_, err = dictModel.Insert()

	resData = dictModel

	return
}

func FindDictWord(Param common.Filter) (resData model.DictWord, err error) {
	var logModel model.DictWord

	resData, err = logModel.Find(Param)

	return
}

func SelectDictWord(Param *common.SelectQueryParam) (total int64, resData []model.DictWord, err error) {
	var logModel model.DictWord

	total, resData, err = logModel.Select(Param)

	return
}

func UpdateDictWord(Data validate.UpdateDictWord) (resData model.DictWord, err error) {

	resData, err = resData.Update(model.DictWord{
		Id:         Data.Id,
		ExtendCode: Data.ExtendCode,
	})
	return
}

func DeleteDictWord(Param *common.Filter) error {

	var logModel model.DictWord

	err := logModel.DeleteByFilter(Param)

	return err

}

func BatchUpdateDictWord(param common.SelectQueryParam, extendCode string) (resData []uint64, err error) {

	param.Fields = "id"
	total, res, err := SelectDictWord(&param)

	if err != nil {
		return resData, err
	}
	resData = make([]uint64, total)
	for index, dictWord := range res {
		resData[index] = dictWord.Id
	}
	result := Mysql.DB.Model(&model.DictWord{}).Where("id in ?", resData).Update("extend_code", extendCode)
	if result.Error != nil {
		return resData, result.Error
	}
	return resData, nil
}

// 批量创建 腾讯词库hook同步关闭 手动批量创建
func BatchCreateDictWord(Data validate.BatchAddDictWord) (resData []model.DictWord, err error) {

	dict, err := FindDict(&common.Filter{
		Filter: map[string]interface{}{
			"dict": map[string]interface{}{
				"condition": "id = ?",
				"val":       Data.DictId,
			},
		},
	})
	if err != nil {
		return resData, err
	}

	resData = make([]model.DictWord, len(Data.Words))
	for index, word := range Data.Words {
		resData[index].DictId = Data.DictId
		resData[index].Word = word
		resData[index].Pos = Data.Pos
		resData[index].ExtendCode = Data.ExtendCode
		resData[index].DontNeedSync = true
	}

	err = Mysql.DB.Model(&model.DictWord{}).Transaction(func(tx *gorm.DB) error {
		result := tx.Create(&resData)
		if result.Error != nil {
			return err
		}
		if dict.DictId != "" {
			wordItems := make([]*nlp.WordItem, len(resData))
			for index, _ := range resData {
				wordItems[index] = &nlp.WordItem{
					Text: &resData[index].Word,
					Pos:  &resData[index].Pos,
				}
			}
			_, err = nlpWord.Create(dict.DictId, &wordItems)
			if err != nil {
				return err
			}
		}
		return nil
	})
	if err != nil {
		return resData, err
	}

	return resData, nil
}
